package com.daiki.onlinerecruitment.component;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author daiki
 * @classname MyLocaleResolver
 * @Description 自定义区域信息解析器
 * @datetime 2021/10/16 14:58
 */
@Component
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取国际化代码
        String lang = request.getParameter("lang");
        //默认使用默认的语言配置文件文件
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(lang)) {
            //将国际化代码中的语言代码和国家代码以‘_’进行分割zh_CN
            String[] split = lang.split("_");
            //使用两个参数的构造器，第一个参数为语言代码，第二个参数为国家代码
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
